package com.breadenglish.cms.api.app.sys.manager;

import com.breadenglish.cms.api.app.common.anotation.EnablePaging;
import com.breadenglish.cms.api.app.common.context.PagingContextHolder;
import com.breadenglish.cms.api.app.common.page.Page;
import com.breadenglish.cms.api.app.common.page.QueryPage;
import com.breadenglish.cms.api.app.domain.constants.MessageConstants;
import com.breadenglish.cms.api.app.domain.db.CodeClass;
import com.breadenglish.cms.api.app.domain.model.ResultMessage;
import com.breadenglish.cms.api.app.domain.query.CodeClassQuery;
import com.breadenglish.cms.api.app.sys.service.CodeClassService;
import com.breadenglish.cms.api.app.sys.service.ComboboxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典分类管理
 * User: guoyao
 * Date: 13-12-17
 * Time: 上午9:18
 */
@Controller
@RequestMapping("/base")
public class CodeClassController {

    private final static Logger logger = LoggerFactory.getLogger(CodeClassController.class);

    @Resource
    private CodeClassService codeClassService;


    /**
     * 字典分类管理主页面
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/forwardQueryCodeClasses.html")
    public String forwordQueryCodeClasses() throws Throwable {
        return "base/code_class/query_code_classes";
    }

    /**
     * 显示字典分类详情页面
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/displayCodeClassTemplate.html")
    public ModelAndView displayCodeClassTemplate(@RequestParam("codeClassId") String codeClassId) throws Throwable {
        ModelAndView mav = new ModelAndView();
        // 分配模板变量 字典分类详情
        mav.addObject("codeClassInfo", codeClassService.queryCodeClass(codeClassId));
        mav.setViewName("base/code_class/display_code_class");
        return mav;
    }

    /**
     * 添加字典分类页面
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/insertCodeClassTemplate.html")
    public ModelAndView insertCodeClassTemplate() throws Throwable {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("base/code_class/insert_code_class");
        return mav;
    }

    /**
     * 修改字典分类页面
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/updateCodeClassTemplate.html")
    public ModelAndView updateCodeClassTemplate(@RequestParam("codeClassId") String codeClassId) throws Throwable {
        ModelAndView mav = new ModelAndView();
        // 分配模板变量 字典分类详情
        CodeClass codeClassInfo = codeClassService.queryCodeClass(codeClassId);
        mav.addObject("codeClassInfo", codeClassInfo);
        //mav.addObject("DELTAG",comboboxService.queryDictByClass("DELTAG", codeClassInfo.getDelTag().toString()));
        mav.setViewName("base/code_class/update_code_class");
        return mav;
    }


    /**
     * 字典分类搜索
     *
     * @param queryPage 分页对象
     * @param codeClass 字典分类对象
     * @return 搜索结果
     */
    @EnablePaging
    @RequestMapping(value = "/queryCodeClasses.html")
    @ResponseBody
    public Map<String, Object> queryCodeClasses(@ModelAttribute("queryPage") QueryPage queryPage, @ModelAttribute("codeClass") CodeClass codeClass) throws Throwable {
        List<CodeClassQuery> result = codeClassService.queryCodeClasses(codeClass);
        Page page = PagingContextHolder.getPage();
        //logger.debug("数据总数: " + page.getTotalResult());
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("total", page.getTotalResult());
        resultMap.put("rows", result);
        return resultMap;
    }

    /**
     * 添加字典分类信息
     *
     * @param codeClass 字典分类对象
     * @return 插入结果
     */
    @RequestMapping(value = "/insertCodeClass.html")
    @ResponseBody
    public ResultMessage insertCodeClass(@ModelAttribute("codeClass") CodeClass codeClass) throws Throwable {
        codeClassService.insertCodeClass(codeClass);
        return CommonUtils.createResultMessage(MessageConstants.INSERT_CODE_CLASS_SUCCESS);
    }

    /**
     * 更新字典分类信息
     *
     * @param codeClass 字典分类对象
     * @return 更新结果
     */
    @RequestMapping(value = "/updateCodeClass.html")
    @ResponseBody
    public ResultMessage updateCodeClass(@ModelAttribute("codeClass") CodeClass codeClass) throws Throwable {
        codeClassService.updateCodeClass(codeClass);
        return CommonUtils.createResultMessage(MessageConstants.UPDATE_CODE_CLASS_SUCCESS);
    }

    /**
     * 删除字典分类信息
     *
     * @param codeClass    字典分类对象
     * @param codeClassIds 要删除的字典分类ID以逗号组合的字符串
     * @return 删除结果
     */
    @RequestMapping(value = "/deleteCodeClasses.html")
    @ResponseBody
    public ResultMessage deleteCodeClass(@ModelAttribute("codeClass") CodeClass codeClass, @RequestParam("delCodeClasses") String codeClassIds) throws Throwable {
        codeClassService.deleteCodeClass(codeClass, codeClassIds);
        return CommonUtils.createResultMessage(MessageConstants.DELETE_CODE_CLASS_SUCCESS);
    }

    /**
     * 验证字典分类ID是否存在
     *
     * @param classId 字典分类ID
     * @return 验证结果
     */
    @RequestMapping(value = "/checkClassId.html")
    @ResponseBody
    public ResultMessage checkClassId(@RequestParam("classId") String classId) throws Throwable {
        codeClassService.uniqueClassId(classId);
        return CommonUtils.createResultMessage(MessageConstants.CODE_CLASS_UNIQUE_CHECK_SUCCESS);
    }
}
